package cn.ac.iie;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

/**
 * @auther Fighter Created on 2016/11/15.
 */
public class InputWindow extends JFrame {
    static {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
    }

    private static final Logger logger = Logger.getLogger(InputWindow.class);

    public JPanel pnlJsonCheck;

    public JLabel lbTitle;
    public JLabel lbDirname;
    public JLabel lbComID;
    public JLabel lbDataType;
    public JLabel lbFieldName;

    public JTextField path;
    public JTextField comId;
    public JTextField dataType;
    public JTextField field;


    public JButton btnSub;
    public JButton btnStop;
    public JButton btnReset;


    public InputWindow() {

        pnlJsonCheck = new JPanel();

        lbTitle = new JLabel();

        lbDirname = new JLabel();
        lbComID = new JLabel();
        lbDataType = new JLabel();
        lbFieldName = new JLabel();


        path = new JTextField();
        comId = new JTextField();
        dataType = new JTextField();
        field = new JTextField();

        btnSub = new JButton();
        btnStop = new JButton();
        btnReset = new JButton();

        userInit();
    }


    public void userInit() {


        this.setSize(420, 290);//设置框架大小为长300,宽200
        this.setResizable(false);//设置框架不可以改变大小
        this.setTitle("企业json字段查看-GUI v0.2");//用户登录设置框架标题

        this.pnlJsonCheck.setLayout(null);//设置面板布局管理
//        this.pnlJsonCheck.setBackground(Color.cyan);//设置面板背景颜色

        this.lbTitle.setText("企业json字段值查看");//设置标签标题
        this.lbTitle.setFont(new Font("宋体", Font.BOLD, 15));//设置标签字体
        this.lbTitle.setForeground(Color.RED);//设置标签字体颜色

        this.lbDirname.setText("目     录:");
        this.lbComID.setText("企 业ID:");
        this.lbDataType.setText("类     型:");
        this.lbFieldName.setText("字段名 :");

        this.btnSub.setText("开始");
        this.btnStop.setText("停止");
        this.btnReset.setText("重置");

        this.lbTitle.setBounds(70 + 60, 15, 150, 30);//设置标签x坐标120,y坐标15,长60,宽20


        this.lbDirname.setBounds(50 + 30, 55, 60, 20);
        this.lbComID.setBounds(50 + 30, 85, 60, 20);
        this.lbDataType.setBounds(50 + 30, 115, 60, 20);
        this.lbFieldName.setBounds(50 + 30, 145, 60, 20);


        this.path.setBounds(110 + 30, 55, 180, 20);
        this.comId.setBounds(110 + 30, 85, 120 + 60, 20);
        this.dataType.setBounds(110 + 30, 115, 120 + 60, 20);
        this.field.setBounds(110 + 30, 145, 120 + 60, 20);

        //开始
        this.btnSub.setBounds(65 , 190, 60, 20);
        this.btnSub.addActionListener(new ActionListener()//匿名类实现ActionListener接口
                                      {
                                          public void actionPerformed(ActionEvent e) {
                                              btnsub_ActionEvent(e);
                                          }
                                      }
        );

        //停止
        this.btnStop.setBounds(185, 190, 60, 20);
        this.btnStop.addActionListener(new ActionListener()//匿名类实现ActionListener接口
                                       {
                                           public void actionPerformed(ActionEvent e) {
                                               btnstop_ActionEvent(e);
                                           }
                                       }
        );

        //重置
        this.btnReset.setBounds(298, 190, 60, 20);
        this.btnReset.addActionListener(new ActionListener()//匿名类实现ActionListener接口
                                        {
                                            public void actionPerformed(ActionEvent e) {
                                                btnreset_ActionEvent(e);
                                            }
                                        }
        );

        this.pnlJsonCheck.add(lbTitle);//加载标签到面板
        this.pnlJsonCheck.add(lbDirname);
        this.pnlJsonCheck.add(lbComID);
        this.pnlJsonCheck.add(lbDataType);
        this.pnlJsonCheck.add(lbFieldName);

        this.pnlJsonCheck.add(path);
        this.pnlJsonCheck.add(comId);
        this.pnlJsonCheck.add(dataType);
        this.pnlJsonCheck.add(field);

        this.pnlJsonCheck.add(btnSub);
        this.pnlJsonCheck.add(btnStop);
        this.pnlJsonCheck.add(btnReset);

        this.add(pnlJsonCheck);//加载面板到框架

        this.setVisible(true);//设置框架可显
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序

        int windowWidth = this.getWidth();                    //获得窗口宽
        int windowHeight = this.getHeight();                  //获得窗口高

        Toolkit kit = Toolkit.getDefaultToolkit();             //定义工具包
        Dimension screenSize = kit.getScreenSize();            //获取屏幕的尺寸

        int screenWidth = screenSize.width;                    //获取屏幕的宽
        int screenHeight = screenSize.height;                  //获取屏幕的高

        this.setLocation(screenWidth / 2 - windowWidth, screenHeight / 2 - windowHeight);//设置窗口居中显示
    }

    public void btnsub_ActionEvent(ActionEvent e) {

        if (TestJson.runningFlag) {
            JOptionPane.showMessageDialog(null, "前一任务尚未结束,请稍后再操作!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        String pathStr = path.getText().trim();
        String comIdStr = comId.getText().trim();
        String dataTypeStr = dataType.getText().trim();
        String fieldStr = field.getText().trim();

        if (pathStr.equals("")) {
            JOptionPane.showMessageDialog(null, "目录不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        } else if (comIdStr.equals("")) {
            JOptionPane.showMessageDialog(null, "企业号不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        } else if (dataTypeStr.equals("")) {
            JOptionPane.showMessageDialog(null, "类型不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        } else if (fieldStr.equals("")) {
            JOptionPane.showMessageDialog(null, "字段名不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
//        else if (true) {
//            this.dispose();
//        }

        else {

            File file = new File(pathStr);
            if (!file.isDirectory()) {
                JOptionPane.showMessageDialog(null, "请填写正确目录名！", "错误", JOptionPane.ERROR_MESSAGE);
                logger.error("目录格式错误");
                return;
            }

            try {
                Integer.parseInt(comIdStr);
            } catch (NumberFormatException ne) {
                logger.error(ne);
                JOptionPane.showMessageDialog(null, "请填写正确企业ID！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            try {

                int tmpDataType = Integer.parseInt(dataTypeStr);
                if (tmpDataType < 1 || tmpDataType > 4)
                    throw new Exception("data_type格式错误!");

            } catch (Exception ne2) {
                logger.error(ne2);
                JOptionPane.showMessageDialog(null, "请填写正确类型！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            TestJson.dirname = pathStr;
            TestJson.comID = comIdStr;
            TestJson.data_type = dataTypeStr;
            TestJson.fieldName = fieldStr;

            new Thread() {
                @Override
                public void run() {
                    Robot robot = null;
                    try {
                        robot = new Robot();
                    } catch (AWTException e1) {
                        e1.printStackTrace();
                    }
                    new Thread(new PressESC(robot)).start();
                    JOptionPane.showMessageDialog(null, "正在扫描，请稍等！", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }.start();

            new Thread() {
                @Override
                public void run() {
                    try {

                        TestJson.beStop = false;
                        TestJson.getValues();
                        JOptionPane.showMessageDialog(null, "扫描完毕！共扫描文件：" + TestJson.fileList.length + "个，共有指定字段" + TestJson.fieldName + "：" + TestJson.fieldCount + "个", "扫描完成", JOptionPane.INFORMATION_MESSAGE);
                        TestJson.clearFlag();

                    } catch (Exception e1) {
                        logger.error(e1);
                        JOptionPane.showMessageDialog(null, "sorry，程序出错了！请查看日志~", "程序错误", JOptionPane.ERROR_MESSAGE);
                        System.exit(-1);
                    }
                }
            }.start();

        }
    }

    //停止扫描
    public void btnstop_ActionEvent(ActionEvent e) {
        TestJson.beStop = true;
    }


    public void btnreset_ActionEvent(ActionEvent e) {
        path.setText("");
        comId.setText("");
        dataType.setText("");
        field.setText("");
    }

    public static void main(String[] args) {
        new InputWindow();
    }

}

class PressESC implements Runnable {

    Robot robot;

    public PressESC(Robot robot) {
        this.robot = robot;
    }

    public void run() {
        robot.delay(3000);
        robot.keyPress(KeyEvent.VK_ESCAPE);
    }
}